home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / charsetOverlay.js < prev    next >
Encoding:
Text File  |  2005-07-25  |  8.7 KB  |  283 lines

  1. function MultiplexHandler(event)
  2. {
  3.     var node = event.target;
  4.     var name = node.getAttribute('name');
  5.  
  6.     if (name == 'detectorGroup') {
  7.         SetForcedDetector(true);
  8.         SelectDetector(event, false);
  9.     } else if (name == 'charsetGroup') {
  10.         var charset = node.getAttribute('id');
  11.         charset = charset.substring('charset.'.length, charset.length)
  12.         SetForcedCharset(charset);
  13.         SetDefaultCharacterSet(charset);
  14.     } else if (name == 'charsetCustomize') {
  15.         //do nothing - please remove this else statement, once the charset prefs moves to the pref window
  16.     } else {
  17.         SetForcedCharset(node.getAttribute('id'));
  18.         SetDefaultCharacterSet(node.getAttribute('id'));
  19.     }
  20. }
  21.  
  22. function MailMultiplexHandler(event)
  23. {
  24.     var node = event.target;
  25.     var name = node.getAttribute('name');
  26.  
  27.     if (name == 'detectorGroup') {
  28.         SelectDetector(event, true);
  29.     } else if (name == 'charsetGroup') {
  30.         var charset = node.getAttribute('id');
  31.         charset = charset.substring('charset.'.length, charset.length)
  32.         MessengerSetForcedCharacterSet(charset);
  33.     } else if (name == 'charsetCustomize') {
  34.         //do nothing - please remove this else statement, once the charset prefs moves to the pref window
  35.     } else {
  36.         MessengerSetForcedCharacterSet(node.getAttribute('id'));
  37.     }
  38. }
  39.  
  40. function ComposerMultiplexHandler(event)
  41. {
  42.     var node = event.target;
  43.     var name = node.getAttribute('name');
  44.  
  45.     if (name == 'detectorGroup') {
  46.         ComposerSelectDetector(event, true);
  47.     } else if (name == 'charsetGroup') {
  48.         var charset = node.getAttribute('id');
  49.         charset = charset.substring('charset.'.length, charset.length)
  50.         EditorSetDocumentCharacterSet(charset);
  51.     } else if (name == 'charsetCustomize') {
  52.         //do nothing - please remove this else statement, once the charset prefs moves to the pref window
  53.     } else {
  54.         SetForcedEditorCharset(node.getAttribute('id'));
  55.     }
  56. }
  57.  
  58. function SetDefaultCharacterSet(charset)
  59. {
  60.     dump("Charset Overlay menu item pressed: " + charset + "\n");
  61.     BrowserSetDefaultCharacterSet(charset);
  62. }
  63.  
  64. function SelectDetector(event, doReload)
  65. {
  66.     dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n");
  67.  
  68.     var uri =  event.target.getAttribute("id");
  69.     var prefvalue = uri.substring('chardet.'.length, uri.length);
  70.     if ("off" == prefvalue) { // "off" is special value to turn off the detectors
  71.         prefvalue = "";
  72.     }
  73.  
  74.     try {
  75.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  76.                              .getService(Components.interfaces.nsIPrefBranch);
  77.         var str =  Components.classes["@mozilla.org/supports-string;1"]
  78.                              .createInstance(Components.interfaces.nsISupportsString);
  79.  
  80.         str.data = prefvalue;
  81.         pref.setComplexValue("intl.charset.detector",
  82.                              Components.interfaces.nsISupportsString, str);
  83.         if (doReload) window.content.location.reload();
  84.     }
  85.     catch (ex) {
  86.         dump("Failed to set the intl.charset.detector preference.\n");
  87.     }
  88. }
  89.  
  90. function ComposerSelectDetector(event)
  91. {
  92.     //dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n");
  93.  
  94.     var uri =  event.target.getAttribute("id");
  95.     var prefvalue = uri.substring('chardet.'.length, uri.length);
  96.     if ("off" == prefvalue) { // "off" is special value to turn off the detectors
  97.         prefvalue = "";
  98.     }
  99.  
  100.     try {
  101.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  102.                              .getService(Components.interfaces.nsIPrefBranch);
  103.         var str =  Components.classes["@mozilla.org/supports-string;1"]
  104.                              .createInstance(Components.interfaces.nsISupportsString);
  105.  
  106.         str.data = prefvalue;
  107.         pref.setComplexValue("intl.charset.detector",
  108.                              Components.interfaces.nsISupportsString, str);
  109.         EditorLoadUrl(GetDocumentUrl());    
  110.     }
  111.     catch (ex) {
  112.         dump("Failed to set the intl.charset.detector preference.\n");
  113.     }
  114. }
  115.  
  116. function SetForcedDetector(doReload)
  117. {
  118.     BrowserSetForcedDetector(doReload);
  119. }
  120.  
  121. function SetForcedCharset(charset)
  122. {
  123.     BrowserSetForcedCharacterSet(charset);
  124. }
  125.  
  126. var gPrevCharset = null;
  127. function UpdateCurrentCharset()
  128. {
  129.     var menuitem = null;
  130.  
  131.     // exctract the charset from DOM
  132.     var wnd = document.commandDispatcher.focusedWindow;
  133.     if ((window == wnd) || (wnd == null)) wnd = window.content;
  134.     menuitem = document.getElementById('charset.' + wnd.document.characterSet);
  135.  
  136.     if (menuitem) {
  137.         // uncheck previously checked item to workaround Mac checkmark problem
  138.         // bug 98625
  139.         if (gPrevCharset) {
  140.             var pref_item = document.getElementById('charset.' + gPrevCharset);
  141.             if (pref_item)
  142.               pref_item.setAttribute('checked', 'false');
  143.         }
  144.         menuitem.setAttribute('checked', 'true');
  145.     }
  146. }
  147.  
  148. function UpdateCurrentMailCharset()
  149. {
  150.     var charset = msgWindow.mailCharacterSet;
  151.     var menuitem = document.getElementById('charset.' + charset);
  152.  
  153.     if (menuitem) {
  154.         menuitem.setAttribute('checked', 'true');
  155.     }
  156. }
  157.  
  158. function UpdateCharsetDetector()
  159. {
  160.     var prefvalue;
  161.  
  162.     try {
  163.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  164.                              .getService(Components.interfaces.nsIPrefBranch);
  165.         prefvalue = pref.getComplexValue("intl.charset.detector",
  166.                                          Components.interfaces.nsIPrefLocalizedString).data;
  167.     }
  168.     catch (ex) {
  169.         prefvalue = "";
  170.     }
  171.  
  172.     if (prefvalue == "") prefvalue = "off";
  173.     dump("intl.charset.detector = "+ prefvalue + "\n");
  174.  
  175.     prefvalue = 'chardet.' + prefvalue;
  176.     var menuitem = document.getElementById(prefvalue);
  177.  
  178.     if (menuitem) {
  179.         menuitem.setAttribute('checked', 'true');
  180.     }
  181. }
  182.  
  183. function UpdateMenus(event)
  184. {
  185.     // use setTimeout workaround to delay checkmark the menu
  186.     // when onmenucomplete is ready then use it instead of oncreate
  187.     // see bug 78290 for the detail
  188.     UpdateCurrentCharset();
  189.     setTimeout(UpdateCurrentCharset, 0);
  190.     UpdateCharsetDetector();
  191.     setTimeout(UpdateCharsetDetector, 0);
  192. }
  193.  
  194. function CreateMenu(node)
  195. {
  196.   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  197.   observerService.notifyObservers(null, "charsetmenu-selected", node);
  198. }
  199.  
  200. function UpdateMailMenus(event)
  201. {
  202.     // use setTimeout workaround to delay checkmark the menu
  203.     // when onmenucomplete is ready then use it instead of oncreate
  204.     // see bug 78290 for the detail
  205.     UpdateCurrentMailCharset();
  206.     setTimeout(UpdateCurrentMailCharset, 0);
  207.     UpdateCharsetDetector();
  208.     setTimeout(UpdateCharsetDetector, 0);
  209. }
  210.  
  211. var gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener);
  212. var gLastBrowserCharset = null;
  213.  
  214. function charsetLoadListener (event)
  215. {
  216.     var charset = window.content.document.characterSet;
  217.  
  218.     if (charset.length > 0 && (charset != gLastBrowserCharset)) {
  219.         gCharsetMenu.SetCurrentCharset(charset);
  220.         gPrevCharset = gLastBrowserCharset;
  221.         gLastBrowserCharset = charset;
  222.     }
  223. }
  224.  
  225.  
  226. function composercharsetLoadListener (event)
  227. {
  228.     var charset = window.content.document.characterSet;
  229.  
  230.  
  231.     if (charset.length > 0 ) {
  232.        gCharsetMenu.SetCurrentComposerCharset(charset);
  233.     }
  234.  }
  235.  
  236. function SetForcedEditorCharset(charset)
  237. {
  238.     if (charset.length > 0 ) {
  239.        gCharsetMenu.SetCurrentComposerCharset(charset);
  240.     }
  241.     EditorSetDocumentCharacterSet(charset);
  242. }
  243.  
  244.  
  245. var gLastMailCharset = null;
  246.  
  247. function mailCharsetLoadListener (event)
  248. {
  249.     if (msgWindow) {
  250.         var charset = msgWindow.mailCharacterSet;
  251.         if (charset.length > 0 && (charset != gLastMailCharset)) {
  252.             gCharsetMenu.SetCurrentMailCharset(charset);
  253.             gLastMailCharset = charset;
  254.         }
  255.     }
  256. }
  257.  
  258. var wintype = document.firstChild.getAttribute('windowtype');
  259. if (window && (wintype == "navigator:browser"))
  260. {
  261.     var contentArea = window.document.getElementById("appcontent");
  262.     if (contentArea)
  263.         contentArea.addEventListener("pageshow", charsetLoadListener, true);
  264. }
  265. else
  266. {
  267.     var arrayOfStrings = wintype.split(":");
  268.     if (window && arrayOfStrings[0] == "mail") 
  269.     {
  270.         var messageContent = window.document.getElementById("messagepane");
  271.         if (messageContent)
  272.             messageContent.addEventListener("pageshow", mailCharsetLoadListener, true);
  273.     }
  274.     else
  275.     if (window && arrayOfStrings[0] == "composer") 
  276.     {
  277.         contentArea = window.document.getElementById("appcontent");
  278.         if (contentArea)
  279.             contentArea.addEventListener("pageshow", composercharsetLoadListener, true);
  280.     }
  281.  
  282. }
  283.